<?php
!defined('IN_AM') && exit('Access Denied');
class control extends base
{
	function control()
	{
		$this->base();
		$this->load_class('album');
		$this->load_class('photo');
		$this->load_class('comment');
		$this->load_class('user');
	}
	function actioncreate()
	{
		if(!empty($_POST))
		{
			if($_POST['submitted'] ==true)
			{
				$_POST = escape($_POST);
				$uid = intval($_SESSION['user_id']);
				$albumname = $_POST['albumname'];
				$description = $_POST['description'];
				if(empty($albumname) ||empty($description))
				{
					$this->redirect('index.php?m=album&a=create',4000,'标题和描述不能为空');
					exit();
				}
				$albumname = mb_substr($albumname,0,20,'utf-8');
				$description = mb_substr($description,0,200,'utf-8');
				$ctime = time();
				$row = array(
							'user_id'=>"$uid",
							'title'=>"$albumname",
							'description'=>"$description",
							'created'=>"$ctime",
							'updatetime'=>"$ctime",
							'url'=>'',		
							);
				$albumid = $_ENV['album']->create($row);
				if($albumid)
				{
					$this->redirect('index.php?m=photo&a=upload&albumid='.$albumid,4000,'相册创建成功');
					exit();
				}
				else
				{
					$this->redirect('index.php?m=album&a=create',4000,'相册创建失败');
					exit();
				}
			}
		}
		$this->view->display('album_create.htm');
	}
	function actionalbumlist()
	{
		if(!isset($_GET['userid']) && !isset($_SESSION['user_id']))
		{
			$this->redirect('index.php',2000,'请选择要进入的相册');
			exit();
		}
		$uid = isset($_GET['userid'])?intval($_GET['userid']):intval($_SESSION['user_id']);
		if($uid==intval($_SESSION['user_id']))
		$owner = 1;
		else
		$owner = 0;	
		$albumdata = $_ENV['album']->albumlist($uid);
		$this->view->assign('uid',$uid);
		$this->view->assign('owner',$owner);
		$this->view->assign('albumdata',$albumdata);
		$this->view->display('album_list.htm');
	}
	function actionview()
	{
		$albumid = isset($_GET['albumid'])?intval($_GET['albumid']):0;
		$cpage = isset($_GET['cpage'])?intval($_GET['cpage']):1;
		$ppage = isset($_GET['ppage'])?intval($_GET['ppage']):1;
		$cperpage  = 5;//每页评论数
		$cf = ($cpage-1)*$cperpage;//查询开始数
		$ppage = isset($_GET['ppage'])?intval($_GET['ppage']):1;
		$pperpage  = 5;
		$pf = ($ppage-1)*$pperpage;
		if($albumid)
		{
			$pnum = $_ENV['photo']->get_count_by_albumid($albumid);
			$photodata = $_ENV['photo']->photolist($albumid,$pf,$pperpage);
			$albumdata = $_ENV['album']->albumdata($albumid);
			if(!$pnum ||!$albumdata)
			{
				$this->redirect('index.php?m=album&a=albumlist',4000,'相册为空');
				exit();
			}
			$uid = $albumdata['user_id'];
			if($uid != $_SESSION['user_id'])
			{
				$row=array('aviewnum'=>'aviewnum+1');
				$_ENV['album']->update($row,$albumid);
			}
			$cnum = $_ENV['comment']->get_count_by_id($albumid,2);//获得评论记录总数
			$commentdata = $_ENV['comment']->comment_list($albumid,2,$cf,$cperpage);
			$username = $_ENV['user']->get_username($uid);
			$photomulti = $this->multi($pnum,$pperpage,$ppage,'index.php?m=album&a=view&albumid='.$albumid,'ppage');
			$commentmulti = $this->multi($cnum,$cperpage,$cpage,'index.php?m=album&a=view&albumid='.$albumid,'cpage');
			$this->view->assign('username',$username);
			$this->view->assign('photomulti',$photomulti);
			$this->view->assign('commentmulti',$commentmulti);
			$this->view->assign('commentdata',$commentdata);
			$this->view->assign('albumdata',$albumdata);
			$this->view->assign('photodata',$photodata);
			$this->view->display('album_view.htm');
		}
		else
		{
		 	$this->redirect('index.php?m=photo&a=upload',4000,'请先上传图片');
		}
	}
	function actiondelete()
	{
		$albumid = intval(getgpc('albumid'),'G');
		$uid = getgpc('user_id','S');
		empty($uid) && exit('无权限');
		$result = $_ENV['album']->delete($albumid,$uid);
		if($result)
		{
			$photodata = $_ENV['photo']->photo_all($albumid);
			$result = $_ENV['photo']->delete_by_albumid($albumid,$uid);
			foreach($photodata as $value)
			{
				$url = $value['url'];
				@unlink(AM_UPLOAD.$url);
				@unlink(AM_UPLOAD.$url.'.thumb.jpg');
			}
			header('Content-Type:text/html,charset=utf-8');
			$this->redirect('index.php?m=album&a=albumlist#',4000,'删除成功');
			exit();
		}
		else
		{
			$this->redirect('index.php',4000,'没有权限');
			exit();
		}
	}
	function actionedit()
	{
		if(getgpc('submitted','P'))
		{
			$_POST = escape($_POST);
			$albumid = intval(getgpc('albumid','P'));
			$albumdata = $_ENV['album']->albumdata($albumid);
			empty($albumdata) && exit('无效相册ID');
			$uid = $albumdata['user_id'];
			if($uid != getgpc('user_id','S'))
			{
				$this->redirect('index.php',4000,'无权限访问该页面');
				exit();
			}
			$albumname = !empty($_POST['albumname'])?$_POST['albumname']:$albumdata['albumname'];
			$adescription = !empty($_POST['adescription'])?$_POST['adescription']:'';
			$row  = array(
			'albumname'=>'\''."$albumname".'\'',
			'adescription'=>'\''."$adescription".'\'',
			);
			$result = $_ENV['album']->update($row,$albumid);
			if($result)
			{
				$this->redirect('index.php?m=album&a=view&albumid='.$albumid,4000,'更新成功');
				exit();
			}
		}
		$albumid = intval(getgpc('albumid','G'));
		$albumdata = $_ENV['album']->albumdata($albumid);
		empty($albumdata) && exit('无效相册ID');
		$this->view->assign('albumdata',$albumdata);
		$this->view->display('album_edit.htm');
	}
	function actionhot()
	{
		$page = is_numeric(getgpc('page','G'))?intval(getgpc('page','G')):'1';
		$perpage =12;
		$from = ($page-1)*$perpage;
		$num = $_ENV['album']->get_count();
		$albumdata = $_ENV['album']->get_hot($from,$perpage);
		$multi = $this->multi($num,$perpage,$page,'index.php?m=album&a=hot','page');
		$this->view->assign('multi',$multi);
		$this->view->assign('albumdata',$albumdata);
		$this->view->display('album_hot.htm');	
	}
	
}
?>